package com.xing.oauth.server.model.mapper;

import com.xing.oauth.server.model.OauthDto;
import com.xing.oauth.server.model.TokenDto;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.web.bind.annotation.DeleteMapping;

/**
 * 天猫token业务 mapper
 */
@Mapper
public interface AligenieTokenMapper {


    /**
     * 通过refreshToken查询token的信息
     *
     * @param refreshToken
     * @return Token
     */
    @Select("select id,ncl_id,refresh_token,access_token,at_expires_in,rt_expires_in,created_dt,updated_dt,aligenie_token" +
            "from aligenie_oauth_token_mst" +
            "where refresh_token = #{refresh_token,jdbcType=VARCHAR}")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "nlc_id", property = "nlcId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "refresh_token", property = "refreshToken", jdbcType = JdbcType.VARCHAR),
            @Result(column = "access_token", property = "accessToken", jdbcType = JdbcType.VARCHAR),
            @Result(column = "at_expires_in", property = "atExpiresIn", jdbcType = JdbcType.INTEGER),
            @Result(column = "rt_expires_in", property = "rtExpiresIn", jdbcType = JdbcType.INTEGER),
            @Result(column = "created_dt", property = "createdDt", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "updated_dt", property = "updatedDt", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "aligenie_token", property = "aligenieToken", jdbcType = JdbcType.VARCHAR)
    })
    TokenDto getTokenByRefreshToken(String refreshToken);

    /**
     * 根据id删除token信息
     *
     * @param id
     */
    @DeleteMapping("delete from aligenie_oauth_token_mst where id = #{id,jdbcType=INTEGER} ")
    void deleteToken(Integer id);

    /**
     * 更新token信息
     *
     * @param refresh
     */
    @Update("update aligenie_oauth_token_mst set  access_token = #{accessToken}," +
            "updated_dt = #{updatedDt } where id = #{ id ,jdbcType=INTEGER} ")
    void updateToken(TokenDto refresh);

    /**
     * 通过code生成token
     *
     * @param code
     * @return
     */
    @Select("select auth_code,nlc_id,created_dt,aligenie_token" +
            "from" +
            "aligenie_oauth_code_mst" +
            "where" +
            "auth_code=#{authCode,jdbcType=VARCHAR}")
    @Results({
            @Result(column = "auth_code", property = "authCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nlc_id", property = "nlcId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_dt", property = "createdDt", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "aligenie_token", property = "aligenieToken", jdbcType = JdbcType.VARCHAR)

    })
    OauthDto getOauthByCode(String code);

    /**
     * 删除code信息
     *
     * @param code
     */
    @Delete("delete aligenie_oauth_code_mst " +
            "where auth_code = #{authCode,jdbcType =VARCHAR}")
    void deleteOauthCode(String code);

    /**
     * 通过阿里加密的token获取token的信息
     * @param aligenieToken
     * @return
     */
    @Select("SELECT id, aligenie_token " +
            "FROM aligenie_oauth_token_mst "+
            "WHERE " +
            "aligenie_token = #{ aligenieToken,jdbcType=VARCHAR } ")
    @Results({
            @Result(column="id", property="id",jdbcType= JdbcType.INTEGER),
            @Result(column="aligenie_token", property="aligenieToken",jdbcType=JdbcType.VARCHAR)
    })
    TokenDto getTokenByAligenieToken(String aligenieToken);

    /**
     * 根据nlcId更新token信息
     *
     * @param token
     */
    @Update("UPDATE aligenie_oauth_token_mst SET nlc_id = #{nlcId,jdbcType=VARCHAR} , " +
            "refresh_token = #{refreshToken,jdbcType=VARCHAR} ," +
            " access_token = #{accessToken,jdbcType=VARCHAR} , " +
            "at_expires_in = #{atExpiresIn,jdbcType=INTEGER}, " +
            "created_dt = #{createdDt,jdbcType=TIMESTAMP}," +
            "updated_dt = #{updatedDt,jdbcType=TIMESTAMP} " +
            "WHERE aligenie_token = #{aligenieToken,jdbcType=VARCHAR}")
    void updateTokenInfo(TokenDto token);


    /**
     * 添加 长短token对
     *
     * @param token
     * @return empty
     */
    @Insert("  INSERT INTO aligenie_oauth_token_mst (" +
            "nlc_id, access_token," +
            "refresh_token,at_expires_in," +
            "rt_expires_in,created_dt," +
            "updated_dt,aligenie_token) " +
            "VALUES( #{nlcId,jdbcType=VARCHAR}," +
            " #{accessToken,jdbcType=VARCHAR}," +
            " #{refreshToken,jdbcType=VARCHAR}," +
            " #{atExpiresIn,jdbcType=INTEGER}," +
            " #{rtExpiresIn,jdbcType=INTEGER}," +
            " #{createdDt,jdbcType=TIMESTAMP}," +
            " #{updatedDt,jdbcType=TIMESTAMP}," +
            " #{aligenieToken,jdbcType=VARCHAR} " +
            " )")
    void saveToken(TokenDto token);
}
